package demo.demo_ack;

import com.rabbitmq.client.*;
import utils.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer02 {

    public final static String QUEUE_NAME = "queue_ack";

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = RabbitMqUtils.getFactory();
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        //接收消息后的处理
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {

            String message= new String(delivery.getBody());
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("接收到消息:"+message);
            /**
             * 处理成功,进行成功应答
             * 1.消息标记 tag
             * 2.是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };

        //接收消息取消后的处理
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        };

        //采用手动应答
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME,autoAck,deliverCallback,cancelCallback);

    }

}
